package com.business.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.business.dao.ActivitiesCarMapper;
import com.business.dao.ActivitiesLoanMapper;
import com.business.dao.ActivitiesMapper;
import com.business.dao.CarBrandMapper;
import com.business.dao.CarModelMapper;
import com.business.dao.CarSeriesMapper;
import com.business.dao.LoanMapper;
import com.business.domain.Activities;
import com.business.domain.ActivitiesCar;
import com.business.domain.ActivitiesCarExample;
import com.business.domain.ActivitiesCarExample.Criteria;
import com.business.domain.ActivitiesExample;
import com.business.domain.ActivitiesLoan;
import com.business.domain.ActivitiesLoanExample;
import com.business.domain.CarBrand;
import com.business.domain.CarModel;
import com.business.domain.CarSeries;
import com.business.domain.Loan;
import com.business.service.ActivitiesService;
import com.sys.dao.AdminProvinceMapper;
import com.sys.domain.AdminProvince;
import com.sys.domain.AdminProvinceExample;
import com.sys.result.PageParam;
import com.sys.result.RequestPageVo;
import com.sys.result.RequestResultVo;


@Service("activitiesService")
public class ActivitiesServiceImpl implements ActivitiesService{

	@Resource
	private ActivitiesMapper activitiesMapper;
	
	@Resource
	private ActivitiesCarMapper activitiesCarMapper;
	
	@Resource
	private ActivitiesLoanMapper activitiesLoanMapper;
	
	@Resource
	private LoanMapper loanMapper;
	
	@Resource
	private AdminProvinceMapper adminProvinceMapper;
	
	@Resource
	private CarBrandMapper carBrandMapper;
	
	@Resource 
	private CarSeriesMapper carSeriesMapper;
	
	@Resource
	private CarModelMapper carModelMapper;

	@Override
	public RequestPageVo<Activities> queryActivitiesList(PageParam param, String type) {
		
		RequestPageVo<Activities> pageVo=new RequestPageVo<Activities>();
		
		ActivitiesExample example=new ActivitiesExample();
		example.setOrderByClause("id desc");
		com.business.domain.ActivitiesExample.Criteria criteria = example.createCriteria();
		
		//模糊查询
		if(!StringUtils.isEmpty(type)){
			criteria.andTypeLike("%"+type+"%");
		}
		
		int count=activitiesMapper.countByExample(example);
		if(count>0){
			example.setPageSize(param.getLimit());
			example.setStartPage(param.getOffset());
			List<Activities> actList=activitiesMapper.selectByExample(example);//活动类型列表
			
			pageVo.setLimit(param.getLimit());
			pageVo.setTotal(count);
			pageVo.setOffset(param.getOffset());
			pageVo.setRows(actList);
		}
		
		return pageVo;
	}

	@Override
	public RequestResultVo<Activities> deleteActivities(Integer id) {
		
		RequestResultVo<Activities> result = new RequestResultVo<Activities>();
		
		try {
			
			int count = activitiesMapper.deleteByPrimaryKey(id);
			if(count>0){
				result.setSuccess(true);
			}else{
				result.setSuccess(false);
				result.setErrorMessage("删除失败!");
			}
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMessage("删除失败!");
		}
		
		return result;
	}

	@Override
	public RequestResultVo<Activities> addActivities(Activities act) {
		RequestResultVo<Activities> result=new RequestResultVo<Activities>();
		try {
			String type=act.getType();//判断活动类型是否已存在
			ActivitiesExample example=new ActivitiesExample();
			com.business.domain.ActivitiesExample.Criteria criteria = example.createCriteria();
			criteria.andTypeEqualTo(type);
			List<Activities> list=activitiesMapper.selectByExample(example);
			if(list.size()>0&&list!=null){
				result.setSuccess(false);
				result.setErrorMessage("添加失败，活动类型不能重复!");
				return result;
			}else{
				activitiesMapper.insertSelective(act);
				result.setSuccess(true);
			}
			
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMessage("添加失败!");
		}
		return result;
	}

	@Override
	public RequestResultVo<Activities> updateActivities(Activities act) {
		RequestResultVo<Activities> result=new RequestResultVo<Activities>();
		try {
			String type=act.getType();//判断活动类型是否已存在
			ActivitiesExample example=new ActivitiesExample();
			com.business.domain.ActivitiesExample.Criteria criteria = example.createCriteria();
			criteria.andTypeEqualTo(type);
			List<Activities> list=activitiesMapper.selectByExample(example);
			
			for(Iterator<Activities> itor=list.iterator();itor.hasNext();){
				Activities activities=itor.next();
				if(activities.getId()!=act.getId()){//活动类型修改了
					if(list.size()>0){
						result.setSuccess(false);
						result.setErrorMessage("活动类型已存在，请勿重复！");
						return result;
					}
				}
			}
			activitiesMapper.updateByPrimaryKeySelective(act);
			result.setSuccess(true);
			
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMessage("更新失败!");
		}
		return result;
	}

	@Override
	public RequestPageVo<ActivitiesCar> queryActivitiesCarList(PageParam param,String activitiesType) {
		RequestPageVo<ActivitiesCar> pageVo=new RequestPageVo<ActivitiesCar>();
		
		ActivitiesCarExample example=new ActivitiesCarExample();
		example.setOrderByClause("id desc");
		Criteria criteria = example.createCriteria();
		
		//根据活动类型条件查询
		if(!StringUtils.isEmpty(activitiesType)){
			criteria.andActivitiesTypeLike("%"+activitiesType+"%");
		}
		
		int count=activitiesCarMapper.countByExample(example);
		if(count>0){
			example.setPageSize(param.getLimit());
			example.setStartPage(param.getOffset());
			List<ActivitiesCar> actList=activitiesCarMapper.selectByExample(example);//车贷活动类型列表
			
			for(ActivitiesCar activitiesCar:actList){
				Integer city=activitiesCar.getCity();//省份id
				if(city!=null){
					AdminProvince province=adminProvinceMapper.selectByPrimaryKey(city.longValue());
					activitiesCar.setAdminProvince(province);
				}
				
				//判断活动属于车型车系还是品牌
				String type=activitiesCar.getCarType();
				if(type.equals("品牌")){
					CarBrand brand=carBrandMapper.selectByPrimaryKey(activitiesCar.getCarId());
					activitiesCar.setBrand(brand);
				}else if(type.equals("车系")){
					CarSeries series=carSeriesMapper.selectByPrimaryKey(activitiesCar.getCarId());
					if(series!=null){
						CarBrand brand=carBrandMapper.selectByPrimaryKey(series.getBrandId());
						activitiesCar.setBrand(brand);
						activitiesCar.setSeries(series);
					}
				}else if(type.equals("车型")){
					CarModel model=carModelMapper.selectByPrimaryKey(activitiesCar.getCarId());
					if(model!=null){
						CarSeries series=carSeriesMapper.selectByPrimaryKey(model.getSeriesId());
						if(series!=null){
							CarBrand brand=carBrandMapper.selectByPrimaryKey(series.getBrandId());
							activitiesCar.setBrand(brand);
							activitiesCar.setSeries(series);
						}
					}
					activitiesCar.setModel(model);
				}
			}
			
			pageVo.setLimit(param.getLimit());
			pageVo.setTotal(count);
			pageVo.setOffset(param.getOffset());
			pageVo.setRows(actList);
		}
		
		return pageVo;
	}

	@Override
	public RequestResultVo<ActivitiesCar> deleteActivitiesCar(Integer id) {
		RequestResultVo<ActivitiesCar> result=new RequestResultVo<ActivitiesCar>();
		try {
			activitiesCarMapper.deleteByPrimaryKey(id);
			result.setSuccess(true);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMessage("删除失败");
		}
		return result;
	}

	@Override
	public RequestResultVo<ActivitiesCar> addActivitiesCar(ActivitiesCar act) {
		RequestResultVo<ActivitiesCar> result=new RequestResultVo<ActivitiesCar>();
		try {
			activitiesCarMapper.insertSelective(act);
			result.setSuccess(true);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMessage("添加失败");
		}
		return result;
	}

	@Override
	public RequestResultVo<ActivitiesCar> updateActivitiesCar(ActivitiesCar act) {
		RequestResultVo<ActivitiesCar> result=new RequestResultVo<ActivitiesCar>();
		try {
			activitiesCarMapper.updateByPrimaryKeySelective(act);
			result.setSuccess(true);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMessage("更新失败!");
		}
		return result;
	}

	@Override
	public RequestPageVo<ActivitiesLoan> queryActivitiesLoanList(PageParam param,String activitiesType) {
		RequestPageVo<ActivitiesLoan> pageVo=new RequestPageVo<ActivitiesLoan>();
		
		ActivitiesLoanExample example=new ActivitiesLoanExample();
		example.setOrderByClause("id desc");
		com.business.domain.ActivitiesLoanExample.Criteria criteria = example.createCriteria();
		
		//车贷活动类型 条件查询
		if(!StringUtils.isEmpty(activitiesType)){
			criteria.andActivitiesTypeLike("%"+activitiesType+"%");
		}
		
		int count=activitiesLoanMapper.countByExample(example);
		if(count>0){
			example.setPageSize(param.getLimit());
			example.setStartPage(param.getOffset());
			List<ActivitiesLoan> actList=activitiesLoanMapper.selectByExample(example);//活动类型列表
			for(ActivitiesLoan activitiesLoan:actList){
				Integer loanId=activitiesLoan.getLoanId();
				Loan loan=loanMapper.selectByPrimaryKey(loanId);
				activitiesLoan.setLoan(loan);
			}
			
			pageVo.setLimit(param.getLimit());
			pageVo.setTotal(count);
			pageVo.setOffset(param.getOffset());
			pageVo.setRows(actList);
		}
		
		return pageVo;
	}

	@Override
	public RequestResultVo<ActivitiesLoan> deleteActivitiesLoan(Integer id) {
		RequestResultVo<ActivitiesLoan> result=new RequestResultVo<ActivitiesLoan>();
		try {
			activitiesLoanMapper.deleteByPrimaryKey(id);
			result.setSuccess(true);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMessage("删除失败");
		}
		return result;
	}

	@Override
	public RequestResultVo<ActivitiesLoan> addActivitiesLoan(ActivitiesLoan act) {
		RequestResultVo<ActivitiesLoan> result=new RequestResultVo<ActivitiesLoan>();
		try {
			activitiesLoanMapper.insertSelective(act);
			result.setSuccess(true);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMessage("添加失败");
		}
		return result;
	}

	@Override
	public RequestResultVo<ActivitiesLoan> updateActivitiesLoan(ActivitiesLoan act) {
		RequestResultVo<ActivitiesLoan> result=new RequestResultVo<ActivitiesLoan>();
		try {
			activitiesLoanMapper.updateByPrimaryKeySelective(act);
			result.setSuccess(true);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMessage("修改失败");
		}
		return result;
	}

	@Override
	public List<Activities> queryAllActivities() {
		List<Activities> list=new ArrayList<Activities>();
		ActivitiesExample example=new ActivitiesExample();
		list=activitiesMapper.selectByExample(example);
		return list;
	}

	@Override
	public List<AdminProvince> queryProvinceList() {
		List<AdminProvince> list=new ArrayList<AdminProvince>();
		AdminProvinceExample example=new AdminProvinceExample();
		list=adminProvinceMapper.selectByExample(example);
		return list;
	}

	@Override
	public Boolean checkLoan(ActivitiesLoan loan) {
		Map<String,Object> map =new  HashMap<String, Object>();
		if(loan.getId()!=null){
			map.put("id", loan.getId());
		}
		map.put("loan_id", loan.getLoanId());
		map.put("begin_time", loan.getBeginTime());
		map.put("end_time", loan.getEndTime());
		return activitiesLoanMapper.checkLoan(map)>0?false:true;
	}

	@Override
	public Boolean checkActiceCar(ActivitiesCar car) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(car.getId()!=null && car.getId()!=0){
			map.put("id", car.getId());
		}
		if(car.getCity()!=0){
			map.put("city_id", car.getCity());
		}
		map.put("car_id", car.getCarId());
		map.put("car_type", car.getCarType());
		map.put("begin_time", car.getBeginTime());
		map.put("end_time", car.getEndTime());
		
		try {
			int count = activitiesCarMapper.checkActiceCar(map);
			if(count>0){
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	
	
	
}
